package com.ruifu.worksheet.controller;

import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.vo.PageResultVO;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.worksheet.service.WsCommonSheetService;
import com.ruifu.worksheet.service.WsCustomizeTypeService;
import com.ruifu.worksheet.util.StringUtil;
import com.ruifu.worksheet.vo.WsCustomizeTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import static com.ruifu.worksheet.util.StringUtil.castClassName;

/**
 * @author yuanyundi
 * @date 2020/04/20
 */
@RestController
@CrossOrigin
@RequestMapping("/wsCustomizeType")
public class WsCustomizeTpyeController {
    @Autowired
    private WsCustomizeTypeService wsCustomizeTypeService;
    @Autowired
    private WsCommonSheetService wsCommonSheetService;


    /**
     * 用于存储登录人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    /**
     * 变更单类型列表查询
     * @param pageNum  当前页面
     * @param pageSize 每页大小
     * @param wsCustomizeTypeVO 查询条件
     * @return PageResultVO 查询集合
     */
    @PostMapping(value = "/findPage/{pageNum}/{pageSize}")
    public PageResultVO findPage(
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize,
            @RequestBody WsCustomizeTypeVO wsCustomizeTypeVO){
        //检查当前页码是否合理
        pageNum = (pageNum > 0) ? pageNum : 1;
        //检查每页大小是否合理
        pageSize = (pageSize >= 5) ? pageSize : 5;
        return wsCustomizeTypeService.findPage(pageNum, pageSize, wsCustomizeTypeVO,cloudUser);
    }

    /**
     * 变更单类型单一查询
     * @param ctTypeId 主键ID
     */
    @GetMapping(value = "/findById/{ctTypeId}")
    public WsCustomizeTypeVO findById(@PathVariable("ctTypeId") Long ctTypeId){
        return wsCustomizeTypeService.findById(ctTypeId,cloudUser);
    }

    /**
     * 变更单类型新增
     * @param wsCustomizeTypeVO 变更单类型
     */
    @PostMapping("/insertEntity")
    public ResultVO insertEntity(@RequestBody WsCustomizeTypeVO wsCustomizeTypeVO){
        try {
            wsCustomizeTypeService.insertEntity(wsCustomizeTypeVO, cloudUser);
            return new ResultVO(true, "添加变更单类型成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "添加变更单类型失败");
        }
    }

    /**
     * 变更单类型编辑
     * @param wsCustomizeTypeVO 变更单类型
     */
    @PostMapping("/updateEntity")
    public ResultVO updateEntity(@RequestBody WsCustomizeTypeVO wsCustomizeTypeVO){
        try {
            wsCustomizeTypeService.updateEntity(wsCustomizeTypeVO, cloudUser);
            return new ResultVO(true, "编辑变更单类型成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "编辑变更单类型失败!");
        }
    }

    /**
     * 变更单类型 启动/停用/删除
     * @param ctTypeId 主键ID
     * @param operFlag 操作标识：0-启用,1-停用,2-删除
     */
    @PostMapping("/operatorEntity/{ctTypeId}/{operFlag}")
    public ResultVO operEntrty(@PathVariable("ctTypeId") Long ctTypeId,@PathVariable("operFlag") int operFlag){
        try {
            wsCustomizeTypeService.operEntity(ctTypeId, operFlag, cloudUser);
            return new ResultVO(true, "变更单"+ StringUtil.castFlag(operFlag)+"成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "变更单"+StringUtil.castFlag(operFlag)+"失败");
        }
    }

    /**
     * 变更单类型 新增/修改 提交校验
     * @param ctClass 类别
     * @param ctName 类型名称
     */
    @GetMapping("/checkSubmit/{ctClass}/{ctName}")
    public ResultVO checkSubmit(
            @PathVariable("ctClass") int ctClass,
            @PathVariable("ctName") String ctName){
           int rows = wsCustomizeTypeService.checkSubmit(ctClass,ctName,cloudUser);
           if(rows == 0){
               return new ResultVO(true, "isOK!");
           }else{
               return new ResultVO(false, "后台存在类别:"+castClassName(ctClass)+",类型名称："+ctName+"的数据，请修改!");
           }
    }

    /**
     * 变更单类型 新增/修改 提交校验
     * @param ctClass 类别
     * @param ctName 类型名称
     */
    @GetMapping("/checkSubmit/{ctCode}")
    public ResultVO checkSubmit(
            @PathVariable("ctCode") String ctCode){
        int rows = wsCustomizeTypeService.checkSubmit(ctCode,cloudUser);
        if(rows == 0){
            return new ResultVO(true, "isOK!");
        }else{
            return new ResultVO(false, "后台存在类型编号:"+ctCode+"的重复数据，请修改!");
        }
    }
}
